﻿using Microsoft.EntityFrameworkCore;
using TuLing.Dto;
using TuLing.Entities;
using TuLing.IServices;

namespace TuLing.Services
{
    public class RBACPermissionService: BaseService, IRBACPermissionService
    {
        public RBACPermissionService(DefaultDbContext context) : base(context) { }

        public async Task<List<PermissionDto>> GetListAsync(string keyword = "")
        {
            if (!string.IsNullOrEmpty(keyword))
            {
                return await _context.Permissions.AsNoTracking().Where(m => m.UniqueCode.Contains(keyword) || 
                m.UniqueName.Contains(keyword)).Select(s => new PermissionDto()
                {
                    Id = s.Id,
                    UniqueCode = s.UniqueCode,
                    UniqueName = s.UniqueName
                }).ToListAsync();
            }
            return await _context.Permissions.AsNoTracking().Select(s => new PermissionDto {
                 Id = s.Id,
                 UniqueCode = s.UniqueCode,
                 UniqueName = s.UniqueName
            }).ToListAsync();
        }
        
        public async Task<PermissionDto?> Get(long id)
        {
            var p = await _context.Permissions.AsNoTracking().FirstOrDefaultAsync(s => s.Id == id);
            if (p == null) 
                return null;

            return new PermissionDto
            {
                Id = p.Id,
                UniqueCode = p.UniqueCode,
                UniqueName = p.UniqueName
            };
        }
        
        public async Task Save(PermissionDto input)
        {
            if (input.Id == 0)
            {
                var existCode = _context.Permissions.Any(m => m.UniqueCode == input.UniqueCode);
                if (existCode) throw new Exception("编码重复了。");

                var existName = _context.Permissions.Any(m => m.UniqueName == input.UniqueName);
                if (existCode) throw new Exception("名称重复了。");

                var permission = new Permission
                {
                    UniqueCode = input.UniqueCode,
                    UniqueName = input.UniqueName
                };
                _context.Permissions.Add(permission);
                await _context.SaveChangesAsync();

                input.Id = permission.Id;
            }
            else
            {
                var existCode = _context.Permissions.Any(m =>  m.Id != input.Id && m.UniqueCode == input.UniqueCode);
                if(existCode) throw new Exception("编码重复了。");

                var existName = _context.Permissions.Any(m => m.Id != input.Id && m.UniqueName == input.UniqueName);
                if (existCode) throw new Exception("名称重复了。");
                
                var first = _context.Permissions.FirstOrDefault(m => m.Id == input.Id);
                if (first != null)
                {
                    first.UniqueCode = input.UniqueCode;
                    first.UniqueName = input.UniqueName;
                    _context.Permissions.Update(first);
                    await _context.SaveChangesAsync();
                }
            }
        }

        public async Task Delete(long id)
        {
            var auth = await _context.Permissions.FirstOrDefaultAsync(s => s.Id == id);
            if (auth == null)
            {
                throw new Exception("没有找到数据。");
            }

            if (auth != null)
            {
                _context.Permissions.Remove(auth);
            }
            await _context.SaveChangesAsync();
        }

    }
}
